package cn.paging.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.paging.dao.MemberDao;
import cn.paging.model.Member;
import cn.paging.util.HibernateUtils;

public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao
{

	/**
	 * query for paging
	 * @param hql (string expression)
	 * @param offset (first record)
	 * @param length (the quantity of record in per page)
	 * @return list (for paging in a page)
	 */
	public List queryForPage(final String hql, final int offset,
			final int length)
	{
		Query query =HibernateUtils.getSession().createQuery(hql);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List list = query.list();
		return list;

	}

	/**
	 * query all records
	 * @return the number of total records
	 */
	public int getAllRowCount(String hql)
	{
		
		int result = 0;

		result = HibernateUtils.getSession().createQuery(hql).list().size();

		return result;
	}

	/**
	 * edit for update
	 * @param id (edit by id)
	 */
	public Object edit(int id)
	{
		System.out.println("memberdaoimpl edit id=" + id);

		Query query = HibernateUtils.getSession().createQuery(
				"from Member m where m.id=:id ").setInteger("id", id);

		return query.iterate().next();

	}

	
	/**
	 * update record by object
	 * @param o (add record by object)
	 */
	public void update(Object o)
	{

		Session session = HibernateUtils.getSession();

		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			session.update(o);
			Member m = (Member) o;
			tx.commit();
			System.out.println("memberDoaImpl updateOrSave Object.name="
					+ m.getName());
			System.out.println("memberDoaImpl updateOrSave Object.password="
					+ m.getPassword());
			System.out.println("memberDoaImpl updateOrSave Object.id="
					+ m.getId());
		}
		catch (HibernateException e)
		{
			e.printStackTrace();
			tx.rollback();
		}
		finally
		{
			HibernateUtils.closeSession(HibernateUtils.getSession());
		}

		System.out.println("memberDoaImpl updateOrSave success");
	}

	/**
	 * add record
	 * @param o (add record by object)
	 */
	public void add(Object o)
	{
		Session session = HibernateUtils.getSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			session.save(o);
			Member m = (Member) o;
			tx.commit();
			System.out.println("memberDoaImpl updateOrSave Object.name="
					+ m.getName());
			System.out.println("memberDoaImpl updateOrSave Object.password="
					+ m.getPassword());
			System.out.println("memberDoaImpl updateOrSave Object.id="
					+ m.getId());
		}
		catch (HibernateException e)
		{
			e.printStackTrace();
			tx.rollback();
		}
		finally
		{
			HibernateUtils.closeSession(HibernateUtils.getSession());
		}

		System.out.println("memberDoaImpl add success");
		
	}
	
	/**
	 * delete record
	 * @param id (delete record by id)
	 */
	public void delete(int id)
	{

		Session session = HibernateUtils.getSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			//			String hql="delete from Member m where m.id=:id";
			//			int queryDelete=HibernateUtils.getSession().createQuery("delete from Member m where m.id=:id")
			//									 .setInteger("id", id)
			//									 .executeUpdate();

			Member m = (Member) session.get(Member.class, id);
			session.delete(m);
			tx.commit();
			System.out.println("MemberDaoImpl delete id=" + id);
		}
		catch (HibernateException e)
		{
			e.printStackTrace();
			tx.rollback();
		}
		finally
		{
			HibernateUtils.closeSession(HibernateUtils.getSession());
		}
		System.out.println("memberDoaImpl  delete");
	}

}